package fr.bda.cloud.storage;

import java.util.ArrayList;
import java.util.List;

public class StorageUnitFactory {

	private final List<StorageUnitFactoryInitializer> storageUnitFactoryInitializers = new ArrayList<>();

	public StorageUnitFactory() {
		storageUnitFactoryInitializers.add(new StorageUnitFactoryInitializer() {
			@Override
			public StorageUnit initStorageUnit(StorageUnitConfiguration storageUnitConfiguration) {
				if (storageUnitConfiguration.getStorageUnitType() == StorageUnitType.LOCAL && LocalStorageUnitConfiguration.class.isInstance(storageUnitConfiguration)) {
					return new LocalStorageUnit((LocalStorageUnitConfiguration) storageUnitConfiguration);
				}
				return null;
			}
		});
	}

	public StorageUnit initStorageUnit(StorageUnitConfiguration storageUnitConfiguration) {
		StorageUnit storageUnit = null;
		for (StorageUnitFactoryInitializer factoryInitializer : storageUnitFactoryInitializers) {
			storageUnit = factoryInitializer.initStorageUnit(storageUnitConfiguration);
			if (storageUnit != null) {
				break;
			}
		}
		return storageUnit;
	}
}
